package api

import (
	"net/http"
	"time"

	"github.com/gin-gonic/gin"
	//"mogutouERP/middleware"
	"mogutouERP/models"
	"mogutouERP/utils/response"
)

// CreateCustormerOrderSub 创建新的客户订单
func CreateCustormerOrderSub(c *gin.Context) {
	//userName := c.GetString(middleware.RequestUserNameKey)
	var param struct {
		GoodName string `json:"goodName"`
		Unit     string `json:"unit"`
		Quantity uint   `json:"quantity" `
		UserName string `json:"userName" `
	}
	err := c.ShouldBindJSON(&param)
	if err != nil {
		c.AbortWithStatusJSON(http.StatusBadRequest, response.Error{Error: err.Error()})
		return
	}

	goods := make([]models.CustormerGoodsSub, 1)
	goods[0].GoodsID = param.GoodName
	goods[0].Number = param.Quantity
	goods[0].Unit = param.Unit

	order := models.CustormerOrderSub{
		Operator:        param.UserName,
		Remarks:         "",
		Amount:          0,
		Name:            "",
		Tel:             "",
		DeliveryAddress: "",
		DeliveryTime:    time.Now().Format("2006-01-02 15:04:05"),
		OrderLine:       "",
		OrderTrade:      "",
	}

	orderInfo, err := models.CreateCustormerOrderSub(&order, goods)
	if err != nil {
		c.AbortWithStatusJSON(http.StatusInternalServerError, response.Error{Error: err.Error()})
		return
	}

	c.JSON(http.StatusCreated, orderInfo)
}

// DeleteCustormerOrderSub 删除订单
func DeleteCustormerOrderSub(c *gin.Context) {
	orderID := c.Param("id")

	err := models.DeleteCustormerOrderSub(orderID)
	if err != nil {
		c.AbortWithStatusJSON(http.StatusInternalServerError, response.Error{Error: err.Error()})
		return
	}

	c.JSON(http.StatusNoContent, gin.H{})
}

// ListCustormerOrdersSub 列举所有订单
func ListCustormerOrdersSub(c *gin.Context) {
	orders, err := models.ListCustormerOrdersSub()
	if err != nil {
		c.AbortWithStatusJSON(http.StatusInternalServerError, response.Error{Error: err.Error()})
		return
	}

	c.JSON(http.StatusOK, orders)
}

// ConfirmCustormerOrderSub 确认订单
func ConfirmCustormerOrderSub(c *gin.Context) {
	orderID := c.Param("id")

	var data struct {
		Freight float32 `json:"freight" `
	}
	err := c.ShouldBindJSON(&data)
	if err != nil {
		c.AbortWithStatusJSON(http.StatusBadRequest, response.Error{Error: err.Error()})
		return
	}

	orderInfo, err := models.ConfirmCustormerOrderSub(orderID, data.Freight)
	if err != nil {
		c.AbortWithStatusJSON(http.StatusInternalServerError, response.Error{Error: err.Error()})
		return
	}

	c.JSON(http.StatusCreated, orderInfo)
}

// UpdateCommodity 更新商品信息
func UpdateSaleOrder(c *gin.Context) {
	custormerOrderID := c.Param("id")

	var data struct {
		Name            string `json:"name"`
		Tel             string `json:"tel"`
		DeliveryAddress string `json:"deliveryAddress"`
		OrderTrade      string `json:"orderTrade" `
		OrderLine       string `json:"orderLine"`
		Remarks         string
	}
	err := c.ShouldBindJSON(&data)
	if err != nil {
		c.AbortWithStatusJSON(http.StatusBadRequest, response.Error{Error: err.Error()})
		return
	}

	custormerOrder := models.CustormerOrderSub{
		Name:            data.Name,
		Tel:             data.Tel,
		DeliveryAddress: data.DeliveryAddress,
		OrderLine:       data.OrderLine,
		OrderTrade:      data.OrderTrade,
		Remarks:         data.Remarks,
	}
	err = models.UpdateSaleOrder(custormerOrderID, &custormerOrder)
	if err != nil {
		c.AbortWithStatusJSON(http.StatusInternalServerError, response.Error{Error: err.Error()})
		return
	}

	c.JSON(http.StatusCreated, gin.H{})
}
